package com.cn.jx.manager.service.query.Impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.cn.jx.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.util.page.PageObj;
import com.cn.jx.manager.dao.DictDataRepository;
import com.cn.jx.manager.po.DictData;
import com.cn.jx.manager.service.query.IDictServiceQry;

import com.jx.enumType.ApplyEnum;
import com.jx.enumType.ResourceTypeEnum;
import com.jx.enumType.SexEnum;
import com.jx.enumType.YesNoEnum;
import com.jx.jpaQuery.QueryCondition;
import com.jx.model.Dict;
import com.jx.util.Constant;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Service("dictServiceQry") 
@Transactional
public class DictServiceImplQry extends BaseService implements IDictServiceQry {

	@Autowired
	private DictDataRepository dictDataDao;

	@Override
	@Transactional(readOnly = true)
	public List<DictData> findDictByType(String dictType) {
		return dictDataDao.findDictDataByType(dictType);
	}

	@Override
	@Transactional(readOnly = true)
	public PageObj<DictData> findDictDataByPageAndParams(QueryCondition cond) throws Exception{
		Specification<DictData> speci = getSpecification(cond);
		Pageable pageable = getPageables(cond);
		Page<DictData> res=dictDataDao.findAll(speci,pageable);
		PageObj<DictData> pageRes=new PageObj<DictData>(res,res.getContent());
		return pageRes;
	}

	@Override
	@Transactional(readOnly = true)
	public DictData findDictByCode(String code) {
		return dictDataDao.findDictByCode(code);
	}

	@Override
	@Transactional(readOnly = true)
	public String findDictNameByCode(String code) {
		DictData dd=findDictByCode(code);
		if(dd!=null && Utility.isNotBlank(dd.getName())){
			return dd.getName();
		}
		return Constant._EMPTY;
	}
	
	@Override
	@Transactional(readOnly = true)
	public Set<Dict> getDictSet(String dictType){
		switch (dictType) {
		case "APPLY":
			return ApplyEnum.getAllEnum();
		case "SEX":
			return SexEnum.getAllEnum();
		case "YON":
			return YesNoEnum.getAllEnum();
		case "RESOURCE":
			return ResourceTypeEnum.getAllEnum();
		default:
			List <DictData> dict=findDictByType(dictType);
			Set<Dict> st=new HashSet<Dict>();
			if(dict!=null && dict.size()>0){
				for(DictData dd:dict){
					Dict mp=new Dict();
					mp.setId(dd.getValue());
					mp.setText(dd.getName());
					st.add(mp);
				}
			}
			return st;
		}
		
	}

}
